Ext.define('qjtravel.controller.Home', {
    extend: 'Ext.app.Controller',

    config: {
        refs: {
            home: 'home',
            homeCarousel: 'home #homeCarousel',
            homeClickCategory: 'home #click_category',
            homeClickCategoryBig: "homeBig #click_category1",
            product: 'product',
            detail: 'ProductDetail',
            activity: 'activity'
        },
        control: {
            homeCarousel: {
                onTap: function (cdaid, id) {
                    if (window.localStorage.getItem("homeCarClick") == null) {
                        window.localStorage.setItem("homeCarClick", '1');
                        if (!id) {
                            cordova.exec(function (message) {
                            }, function (error) {
                            }, "Messages", "showMsg", ['没有相关项目']);
                            window.localStorage.removeItem("homeCarClick");
                            return;
                        }
                        Ext.Viewport.mask({xtype: 'loadmask' });
                        localStorage.setItem('cpid', id);
                        localStorage.setItem('isHome', 0);
                        var detail = Ext.getStore('ProductDetail');
                        var product_detail = this.getDetail();
                        if (product_detail == null) {
                            product_detail = Ext.create('qjtravel.view.ProductDetail');
                        }
                        var label = product_detail.query('label');
                        detail.load({
                            params: {cpid: id},
                            callback: function (data) {
                                Ext.each(data, function (detail) {
                                    Ext.each(label, function (elem) {
                                        elem.setData(detail.data);
                                    });
                                    Ext.getCmp('toolbars').setTitle(data[0].get('name'));
                                    data[0].get('tel') == ' ' ? Ext.getCmp('tel').hide() : product_detail.query('#tel')[0].setHtml('<div style="float: left;margin-right: 10px;"><img src="resources/images/tel2.png" height="20px" style=" vertical-align: middle"/></div>' + data[0].get('tel'));
                                    data[0].get('address') == ' ' ? Ext.getCmp('address').hide() : product_detail.query('#address')[0].setHtml('<div style="float: left;margin-right: 10px;"><img src="resources/images/postion.png" height="20px" style=" vertical-align: middle"/></div>' + data[0].get('address'));
                                    if (data[0].get('tel') == ' ' && data[0].get('address') == ' ') {
                                        Ext.getCmp('fieldset').hide()
                                    }
                                    Ext.getCmp('callNumber').setValue(data[0].get('tel'));

                                    console.log(data[0].get('picurl'))
                                    product_detail.query('#picurl')[0].setValue(data[0].get('picurl'));
                                    product_detail.query('#explain')[0].setValue(data[0].get('explain'));
                                    product_detail.query('#name')[0].setValue(data[0].get('name'));

                                    var myAdPicItems = [];
                                    myAdPicItems.push({
                                        xtype: 'panel',
                                        items: {
                                            html: "<img src='" + data[0].get('picurl') + "' style='width:100%;height:100%;' / >"
                                        }
                                    });
                                    Ext.getCmp("detailCarousel").setItems(myAdPicItems);
                                    Ext.getCmp("detailCarousel").setActiveItem(0);

                                });
                            }
                        })
                        console.log(product_detail)
                        Ext.Viewport.animateActiveItem(product_detail, {type: 'slide', direction: 'left'});
                    }
                }
            },
            homeClickCategoryBig: {
                itemtap: function (data, index, item, record) {
                    this.categoryDetailFn(record)
                }
            },
            homeClickCategory: {
                itemtap: function (data, index, item, record) {
                    this.categoryDetailFn(record)
                }
            }
        }
    },
    categoryDetailFn: function (record) {
        if (window.localStorage.getItem("homeClick") == null) {
            Ext.Viewport.mask({xtype: 'loadmask' });
            window.localStorage.setItem("homeClick", '1');
            if (record.data.ccid == 14) {
                var activity = this.getActivity();
                if (activity == null) {
                    activity = Ext.create('qjtravel.view.Activity');
                }
                Ext.Viewport.animateActiveItem(activity, {type: 'slide', direction: 'left'});
            } else if (record.data.ccid == 19) {
                window.open("http://m.ctrip.com/", '_blank', 'location=no');
                Ext.Viewport.unmask();
                return false;
            } else {
                if (Ext.getStore('Product').getData().length == 0) {
                    Ext.getStore('Product').load();
                }
                console.log(record.data.ccid)
                Ext.getStore('Product').filter('ccid', record.data.ccid);
                var product = this.getProduct();
                if (product == null) {
                    product = Ext.create('qjtravel.view.Product');
                }
                Ext.getCmp('toolbar').setTitle(record.data.name);
                Ext.Viewport.animateActiveItem(product, {type: 'slide', direction: 'left'});
            }
        }
    }
});